import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/Index'
import Edit from '@/components/Edit'
import EditTwo from '@/components/Edit-two'
import EditThree from '@/components/Edit-three'
import Show from '@/components/Show'
import ShowResult from '@/components/ShowResult'
import TextEdit from '@/components/TextEdit'
import Question from '@/components/Question'
import UploadImg from '@/components/UploadImg'
import Message from '@/components/Message'
import MessageTo from '@/components/MessageTo'
import Test from '@/components/Test'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/test',
      name: 'Test',
      component: Test,
      meta: {title: '测试'}
    },
    {
      path: '/message',
      name: 'Message',
      component: Message,
      meta: {title: '发消息'}
    },
    {
      path: '/messageto',
      name: 'MessageTo',
      component: MessageTo,
      meta: {title: '选择人群'}
    },
    {
      path: '/uploadimg',
      name: 'UploadImg',
      component: UploadImg,
      meta: {title: '上传图片'}
    },
    {
      path: '/show-result',
      name: 'ShowResult',
      component: ShowResult,
      meta: {title: '投票结果显示'}
    },
    {
      path: '/question',
      name: 'Question',
      component: Question,
      meta: {title: '创建题目'}
    },
    {
      path: '/texedit',
      name: 'TextEdit',
      component: TextEdit,
      meta: {title: '添加文字'}
    },
    {
      path: '/edit-one',
      name: 'Edit',
      component: Edit,
      meta: {title: '基本信息'}
    },
    {
      path: '/edit-three',
      name: 'EditThree',
      component: EditThree,
      meta: {title: '选择题型'}
    },
    {
      path: '/edit-two',
      name: 'EditTwo',
      component: EditTwo,
      meta: {title: '选择题型'}
    },
    {
      path: '/show',
      name: 'Show',
      component: Show,
      meta: {title: '预览'}
    },
    {
      path: '/',
      name: 'Index',
      component: Index,
      meta: {title: '投票'}
    },
    {
      path: '/HelloFromVux',
      name: 'HelloFromVux',
      component: resolve => require(['../components/HelloFromVux'], resolve),
      meta: {title: '投票'}
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    /* 初次刷新第一次返回 savePosition 为undefine */
    if (savedPosition !== null) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})
router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? to.meta.title : ''
  setTimeout(function () {
    next()
  }, 1)
})
export default router
